﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TfsBugger.Core
{
    public class TfsServerManager : IComparable<TfsServerManager>, IComparable
    {
        public TfsServerManager(string serverName, bool useHttps, string portNum)
        {
            m_serverName = serverName;
            m_useHttps = useHttps;
            m_portNum = portNum;
        }


        #region Events/Helpers
        public event EventHandler UserAuthenticated;


        private void RaiseUserAuthenticated()
        {
            if (null != UserAuthenticated)
                UserAuthenticated(this, EventArgs.Empty);
        }
        #endregion


        #region Fields
        private string m_serverName = string.Empty;
        private bool m_useHttps = true;
        private string m_portNum;
        private TeamFoundationServer m_server;
        private List<Project> m_projList = new List<Project>(); 
        #endregion


        public string ServerName
        {
            get { return m_serverName; }
        }


        public bool UseHttps
        {
            get { return m_useHttps; }
        }


        public string PortNum
        {
            get { return m_portNum; }
        }


        public TeamFoundationServer Server
        {
            get { return m_server; }
        }


        public Project[] Projects
        {
            get { return m_projList.ToArray(); }
        }


        public bool IsLoaded
        {
            get { return null != m_server && m_server.HasAuthenticated && m_projList.Count > 0; }
        }


        public bool Load()
        {       
            StringBuilder nameB = new StringBuilder();
            if (m_useHttps)
                nameB.Append("https://");

            nameB.Append(m_serverName.ToString());
            if (!string.IsNullOrEmpty(m_portNum))
                nameB.AppendFormat(":{0}", m_portNum);

            var icp = new UICredentialsProvider();
            m_server = TeamFoundationServerFactory.GetServer(nameB.ToString(), icp);

            //HACK: Temporary hack to avoid having to use ui credentials provider 
            //on each app run 
            //NetworkCredential creds = new NetworkCredential("Zoetermeer_cp", "qytiuo", "snd");
            //App.TfsServer = new TeamFoundationServer("https://" + m_serverName + ":443", creds);

            m_server.Authenticate();
            RaiseUserAuthenticated();
            var wiStor = (WorkItemStore)m_server.GetService(typeof(WorkItemStore));
            m_projList.Clear();
            m_projList.AddRange(wiStor.Projects.Cast<Project>());

            return true;
        }


        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(m_useHttps ? "https://" : "http://");
            sb.Append(m_serverName);
            if (!string.IsNullOrEmpty(m_portNum))
                sb.AppendFormat(":{0}", m_portNum);

            return sb.ToString();
        }


        #region IComparable<TfsServerManager> Members
        public int CompareTo(TfsServerManager other)
        {
            if (null == other)
                return -1;
            return ServerName.ToLower().CompareTo(other.ServerName.ToLower());
        }
        #endregion


        #region IComparable Members
        public int CompareTo(object obj)
        {
            return CompareTo(obj as TfsServerManager);
        }
        #endregion
    }
}
